package com.example.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.example.home.activity.LoginActivity;
import com.example.home.activity.MainActivity;
import com.example.home.activity.R;
import com.example.home.adapter.CartListAdapter;
import com.example.home.api.ApiService;
import com.example.home.interfaces.ResponseCallback;
import com.example.home.viewmodel.ResponseData;
import com.example.home.viewmodel.cart.CartData;
import com.example.home.viewmodel.cart.CartList;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CartFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CartFragment extends Fragment {

    private GridView viewCart;
    private List<CartList> cartLists;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

    public CartFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CartFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CartFragment newInstance(String param1, String param2) {
        CartFragment fragment = new CartFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_cart, container, false);
        Boolean login_status = requireActivity().getIntent().getBooleanExtra("login_status",false);
        if(!login_status){
            Intent intent = new Intent(view.getContext(),LoginActivity.class);
            startActivity(intent);
            requireActivity().finish();
            return null;
        }
        SharedPreferences sp = requireContext().getSharedPreferences("user_info",Context.MODE_PRIVATE);
        String token = sp.getString("token","");
        Log.d("TAG", "onCreate: "+token );
        viewCart = view.findViewById(R.id.cart_list);
        ApiService.GetCartList(token, new ResponseCallback<ResponseData<CartData>>() {
            @Override
            public void onSuccess(ResponseData<CartData> result) {
                cartLists = result.getData().getCartList();
                viewCart.setAdapter(new CartListAdapter(view.getContext(),cartLists));
            }
        });


        return view;
    }
}